import request from '@/utils/request';

// 用户管理接口类型定义
export interface User {
  id: string | number;
  username: string;
  name?: string;
  mobile?: string;
  email?: string;
  avatar?: string;
  status?: number;
  deptId?: string | number;
  remark?: string;
  loginIp?: string;
  loginDate?: string;
  creator?: string;
  createTime?: string;
  updater?: string;
  updateTime?: string;
}

export interface UserPageParams {
  username?: string;
  name?: string;
  mobile?: string;
  pageNo: number;
  pageSize: number;
}

export interface UserPageResult {
  list: User[];
  total: string | number;
}

export interface UserCreateParams {
  username: string;
  password: string;
  name?: string;
  mobile?: string;
  email?: string;
  avatar?: string;
  deptId?: string | number;
  status?: number;
  remark?: string;
}

export interface UserUpdateParams extends Omit<UserCreateParams, 'password'> {
  id: string | number;
}

// 角色管理接口类型定义
export interface Role {
  id: string | number;
  code: string;
  name: string;
  createTime?: string;
  updateTime?: string;
}

export interface RolePageParams {
  code?: string;
  name?: string;
  pageNo: number;
  pageSize: number;
}

export interface RolePageResult {
  list: Role[];
  total: string | number;
}

export interface RoleCreateParams {
  code: string;
  name: string;
}

export interface RoleUpdateParams extends RoleCreateParams {
  id: string | number;
}

// 菜单管理接口类型定义
export interface Menu {
  id: string | number;
  pid: string | number;
  name: string;
  icon?: string;
  path?: string;
  component?: string;
  redirect?: string;
  permission?: string;
  type: 'M' | 'C' | 'F'; // M目录, C菜单, F按钮
  visible: boolean;
  status: boolean;
  sort: number;
  remark?: string;
  createTime?: string;
  updateTime?: string;
}

export interface MenuPageParams {
  pid?: string | number;
  name?: string;
  path?: string;
  type?: 'M' | 'C' | 'F';
  visible?: boolean;
  status?: boolean;
  pageNo: number;
  pageSize: number;
}

export interface MenuPageResult {
  list: Menu[];
  total: string | number;
}

export interface MenuCreateParams {
  pid: string | number;
  name: string;
  icon?: string;
  path: string;
  component?: string;
  redirect?: string;
  permission?: string;
  type: 'M' | 'C' | 'F';
  visible: boolean;
  status: boolean;
  sort: number;
  remark?: string;
}

export interface MenuUpdateParams extends MenuCreateParams {
  id: string | number;
}

// 分页查询用户
export const getUserPage = (params: UserPageParams) => {
  return request.post<{
    code: number;
    message: string;
    data: UserPageResult;
    timestamp?: string;
  }>('/sys/user/page', params);
};

// 根据ID获取用户
export const getUserById = (id: string | number) => {
  return request.get<{
    code: number;
    message: string;
    data: User;
  }>('/sys/user/get', {
    params: { id }
  });
};

// 创建用户
export const createUser = (params: UserCreateParams) => {
  return request.post<{
    code: number;
    message: string;
    data: number;
  }>('/sys/user/create', params);
};

// 更新用户
export const updateUser = (params: UserUpdateParams) => {
  return request.put<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/user/update', params);
};

// 删除用户
export const deleteUser = (ids: string | number | (string | number)[]) => {
  const idArray = Array.isArray(ids) ? ids : [ids];
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/user/delete', idArray);
};

// 角色管理接口
// 分页查询角色
export const getRolePage = (params: RolePageParams) => {
  return request.post<{
    code: number;
    message: string;
    data: RolePageResult;
    timestamp?: string;
  }>('/sys/role/page', params);
};

// 查询角色列表
export const getRoleList = (params?: { code?: string; name?: string }) => {
  return request.post<{
    code: number;
    message: string;
    data: Role[];
  }>('/sys/role/list', params || {});
};

// 根据ID获取角色
export const getRoleById = (id: string | number) => {
  return request.get<{
    code: number;
    message: string;
    data: Role;
  }>(`/sys/role/${id}`);
};

// 创建角色
export const createRole = (params: RoleCreateParams) => {
  return request.post<{
    code: number;
    message: string;
    data: number;
  }>('/sys/role/create', params);
};

// 更新角色
export const updateRole = (params: RoleUpdateParams) => {
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/role/update', params);
};

// 删除角色
export const deleteRole = (ids: string | number | (string | number)[]) => {
  const idArray = Array.isArray(ids) ? ids : [ids];
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/role/delete', idArray);
};

// 用户角色管理接口
// 获取用户角色列表
export const getUserRoles = (userId: string | number) => {
  return request.get<{
    code: number;
    message: string;
    data: Role[];
  }>(`/sys/user/roles/${userId}`);
};

// 分配用户角色
export const assignUserRoles = (userId: string | number, roleIds: (string | number)[]) => {
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/user/assign-roles', {
    userId,
    roleIds
  });
};

// 菜单管理接口
// 分页查询菜单
export const getMenuPage = (params: MenuPageParams) => {
  return request.post<{
    code: number;
    message: string;
    data: MenuPageResult;
    timestamp?: string;
  }>('/sys/menu/page', params);
};

// 查询菜单列表
export const getMenuList = (params?: { pid?: string | number; name?: string; path?: string; type?: 'M' | 'C' | 'F'; visible?: boolean; status?: boolean }) => {
  return request.post<{
    code: number;
    message: string;
    data: Menu[];
  }>('/sys/menu/list', params || {});
};

// 查询菜单树形结构
export const getMenuTree = (params?: { pid?: string | number; name?: string; path?: string; type?: 'M' | 'C' | 'F'; visible?: boolean; status?: boolean }) => {
  return request.post<{
    code: number;
    message: string;
    data: Menu[];
  }>('/sys/menu/tree', params || {});
};

// 根据ID获取菜单
export const getMenuById = (id: string | number) => {
  return request.get<{
    code: number;
    message: string;
    data: Menu;
  }>(`/sys/menu/${id}`);
};

// 创建菜单
export const createMenu = (params: MenuCreateParams) => {
  return request.post<{
    code: number;
    message: string;
    data: number;
  }>('/sys/menu/create', params);
};

// 更新菜单
export const updateMenu = (params: MenuUpdateParams) => {
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/menu/update', params);
};

// 删除菜单
export const deleteMenu = (ids: string | number | (string | number)[]) => {
  const idArray = Array.isArray(ids) ? ids : [ids];
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/menu/delete', idArray);
};

// 查询子菜单列表
export const getMenuChildren = (pid: string | number) => {
  return request.get<{
    code: number;
    message: string;
    data: Menu[];
  }>(`/sys/menu/children/${pid}`);
};

// 查询用户列表
export const getUserList = (params?: { username?: string; name?: string }) => {
  return request.post<{
    code: number;
    message: string;
    data: User[];
  }>('/sys/user/list', params || {});
};

// 查询简单用户列表
export const getUserSimpleList = (params?: { username?: string; name?: string }) => {
  return request.post<{
    code: number;
    message: string;
    data: User[];
  }>('/sys/user/simple-list', params || {});
};

// 获取角色用户列表
export const getRoleUsers = (roleId: string | number) => {
  return request.get<{
    code: number;
    message: string;
    data: User[];
  }>(`/sys/role/users/${roleId}`);
};

// 分配角色用户
export const assignRoleUsers = (roleId: string | number, userIds: (string | number)[]) => {
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/role/assign-users', {
    roleId,
    userIds
  });
};

// 获取角色菜单列表
export const getRoleMenus = (roleId: string | number) => {
  return request.get<{
    code: number;
    message: string;
    data: (string | number)[];
  }>(`/sys/role/menus/${roleId}`);
};

// 分配角色菜单
export const assignRoleMenus = (roleId: string | number, menuIds: (string | number)[]) => {
  return request.post<{
    code: number;
    message: string;
    data: boolean;
  }>('/sys/role/assign-menus', {
    roleId,
    menuIds
  });
};

// 用户权限数据接口类型（不包含用户基本信息）
export interface UserPermissionData {
  menus: Menu[];
  permissions: string[];
  roles: string[];
}

// 获取用户路由权限
export const getUserRoutes = () => {
  return request.get<{
    code: number;
    message: string;
    data: UserPermissionData;
  }>('/sys/user/routes');
};

// 获取用户菜单树（复用用户路由权限接口）
export const getUserMenuTree = () => {
  return request.get<{
    code: number;
    message: string;
    data: Menu[];
  }>('/sys/user/routes').then(response => ({
    code: response.code,
    message: response.message,
    data: response.data.menus
  }));
};

// 检查用户权限（前端直接检查，无需后端接口）
// 使用方式：const { hasPermission } = usePermission(); hasPermission('system:user:list');